<template>
  <div class="vc-page">
    <!-- 查询条件 -->
    <query-terms :searchConfig="searchConfig"/>

    <!-- 列表展示 -->
    <div class="vc-table">
      <tables
        v-model="tableConfig.tableData"
        :columns="tableConfig.columns"
        :stripe="true"/>

      <el-pagination
        :current-page="currentPage"
        @current-change="handleCurrentChange"
        :page-size="GLOBAL.QUERY.PAGE_SIZE"
        :total="total"
        layout="total, prev, pager, next">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AuditBase',
  data () {
    return {
      currentPage: 1,
      total: 0
    }
  },
  created () {
    this.findTableDataByFuzzyPage()
  },
  methods: {
    /**
     * 加载列表数据
     */
    findTableDataByFuzzyPage (pageNum = this.currentPage, pageSize = this.GLOBAL.QUERY.PAGE_SIZE, entity = this.searchData) {
      this.$api[this.api]({
        data: {
          pageNum: pageNum,
          pageSize: pageSize,
          params: entity
        }
      }).then(res => {
        if (!res.success || !res.data.total) {
          this.emptyTableData()
          return
        }
        if (!res.data.records || !res.data.records.length) {
          this.currentPage--
          this.findTableDataByFuzzyPage()
          return
        }
        this.tableConfig.tableData = res.data.records
        this.total = res.data.total
        this.currentPage = res.data.current
      }).catch(() => {
        this.emptyTableData()
      })
    },
    /**
     * 清空表格数据
     */
    emptyTableData () {
      this.tableConfig.tableData = []
      this.total = 0
      this.currentPage = 1
    },
    /**
     * 格式化列表数据
     */
    indexMethod (index) {
      return this.GLOBAL.formatter.formatIndex(index, this.currentPage)
    },
    /**
     * 点击页数
     */
    handleCurrentChange (val) {
      this.findTableDataByFuzzyPage(val)
    },
    /**
     * 查询
     */
    search (params) {
      let list = params.createTime || []
      this.searchData = Object.assign(list.length ? {
        startTime: list[0],
        endTime: list[1] + 86399000
      } : {}, this.GLOBAL.filter.filterSpace(params), { createTime: '' })
      this.currentPage = 1
      this.findTableDataByFuzzyPage()
    },
    /**
     * 重置
     */
    reset () {
      this.search(this.GLOBAL.object.clearValOfObj(this.searchData))
    }
  }
}
</script>
